package com.zjy.pgcms.util;

import com.zjy.pgcms.common.BaseResponse;
import com.zjy.pgcms.entity.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 音音音
 */
public class ValidationUtil {
    /**
     * 验证用户名是否合法，(字母开头，允许5-16字节，允许字母数字下划线)
     * @param username
     * @return
     */
    public static Boolean usernameValidation(String username){
        return username.matches("^[a-zA-Z][a-zA-Z0-9_]{4,15}$");
    }

    /**
     * 验证密码是否合法,强密码(必须包含大小写字母和数字的组合，不能使用特殊字符，长度在8-10之间)
     * @param password
     * @return
     */
    public static Boolean strongPasswordValidation(String password){
        return password.matches("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$");
    }

    /**
     * 中文、英文、数字但不包括下划线等符号
     * @param name
     * @return
     */
    public static Boolean nameValidation(String name){
        return name.matches("^[\\u4E00-\\u9FA5A-Za-z0-9]+$ 或 ^[\\u4E00-\\u9FA5A-Za-z0-9]{2,20}$");
    }

    /**
     * 0/1
     * @param gender
     * @return
     */
    public static Boolean genderValidation(Byte gender){
        if (gender == 0 || gender == 1){
            return true;
        }
        return false;
    }

    public static Boolean phoneValidation(String phone){
        return phone.matches("^1[3,4,5,6,7,8,9][0-9]{9}$");
    }


    public static Boolean emailValidation(String name){
        return name.matches("^\\\\s*\\\\w+(?:\\\\.{0,1}[\\\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\\\.[a-zA-Z]+\\\\s*$");
    }





    public static Boolean hasLoginUser(HttpSession session){
        User user = (User)session.getAttribute("user");
        if (user == null){
            return false;
        }
        return true;
    }

    public static Boolean hasNotLogin(HttpServletRequest request){
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if (user == null){
            return true;
        }
        return false;
    }
}
